<style scoped>
    .company >>> .layui-form-select {
        z-index: auto;
    }
</style>

<template>
    <div class="company">
        <fieldset class="layui-elem-field layui-field-title">
            <legend>添加批次</legend>
        </fieldset>
        <form class="layui-form " >
<!--            <div class="layui-form-item">-->
<!--                <label class="layui-form-label">编号</label>-->
<!--                <div class="layui-input-block">-->
<!--                    <input type="text" name="code" lay-verify="required" placeholder="请输入批次编号" class="layui-input">-->
<!--                </div>-->
<!--            </div>-->
            <div class="layui-form-item">
                <label class="layui-form-label">名称</label>
                <div class="layui-input-block">
                    <input type="text" name="name" lay-verify="required" placeholder="请输入批次名称" class="layui-input">
                </div>
            </div>
           <!-- <div class="layui-form-item">
                <label class="layui-form-label">采收主体</label>
                <div class="layui-input-block">
                    <input type="hidden" name="orgguid" ref="orgguid" />
                    <select type="text" name="batchmain" lay-verify="required" lay-filter="batchmain" class="layui-select" lay-search>
                        <option value="">请选择</option>
                        <option v-for="(item, index) in companys" :value="item.name">{{item.name}}</option>
                    </select>
                </div>
            </div>-->
            <div class="layui-form-item">
                <label class="layui-form-label">部门 </label>
                <div class="layui-input-block">
                    <input type="hidden" name="departname" ref="departname" />
                    <select name="departid" lay-filter="depart" lay-verify="required"  class="layui-select" lay-search>
                        <option value="">请选择部门</option>
                        <option v-for="(item, index) in departments" :key="item.guid" :value="item.guid">{{item.name}}</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">品种说明</label>
                <div class="layui-input-block">
                    <input type="text" name="variety" placeholder="请输入品种说明" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">计划数量</label>
                <div class="layui-input-block">
                    <input type="number" name="plannumber" placeholder="请输入计划数量" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">采收时间</label>
                <div class="layui-input-inline" style="width: 200px;">
                    <input type="text" name="starttime" id="starttime" placeholder="开始时间" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid">-</div>
                <div class="layui-input-inline" style="width: 200px;">
                    <input type="text" name="endtime" id="endtime" placeholder="结束时间" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">质检方式</label>
                <div class="layui-input-block">
                    <input type="radio" checked name="mode" value="1" title="库房质检">
                    <input type="radio" name="mode" value="2" title="地头质检">
                </div>
            </div>
            <!-- <div class="layui-form-item">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <input type="radio" checked name="status" value="1" title="进行中">
                    <input type="radio" name="status" value="2" title="否">
                </div>
            </div> -->
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="submit">立即提交</button>
                    <button class="layui-btn layui-btn-primary" @click="$parent.back">返回</button>
                </div>
            </div>

        </form>
    </div>
</template>

<script>
    let qs = require('qs');

    export default {
        name: 'AddBatch',
        data () {
            return {
                companys: [],
                departments: []
            }
        },
        mounted() {
            let that = this;
            that.loadOrganization();
            that.getDeparts();
            layui.use(['form'], function () {
                var form = layui.form,
                    layer = layui.layer;

                //提交编辑过的数据
                form.on('submit(submit)', function (data) {
                    let loader = layer.load();
                    $.ajax({
                        url: '/Controller/harvest_Procurementbatch!save.action',
                        data: data.field,
                        type: "post",
                        dataType: "json",
                        success: function(res) {
                            if (res.msg == "success") {
                                that.toast("修改成功");
                                that.$parent.switchPage('BatchList');
                            } else {
                                that.toast("系统错误");
                            }
                        },
                        complete: function () {
                            layer.close(loader);
                        }
                    });
                    return false;
                });

                form.on('select(depart)', function (data) {
                    that.$refs.departname.value = that.departments.filter(item => {
                        return item.guid == data.value;
                    })[0].name;
                })
                form.on('select(batchmain)', function (data) {
                    that.$refs.orgguid.value = that.companys.filter(item => {
                        return item.name == data.value;
                    })[0].guid;
                })
            });
            layui.use('laydate', function(){
                var laydate = layui.laydate;
                laydate.render({
                    elem: '#starttime',
                });
                laydate.render({
                    elem: '#endtime',
                });
            });
        },
        updated() {
            layui.form.render();
        },
        methods: {
            loadOrganization: function () {
                this.axios({
                    method: 'post',
                    url: '/sales/Organization!listAll.action'
                }).then((response) => {
                    let res = response.data;
                    if (res.info != undefined && res.info != 'error' ) {
                        this.companys = res.info;
                        // if (this.adata != '') {
                        //     this.$refs.batchmain.value = this.adata.batchmain;
                        // }
                    } else {
                        that.toast("系统错误");
                    }
                })
            },
            getDeparts: function() {
                this.$post('/sales/Department!listAll.action')
                    .then(response => {
                        let data = response.data;
                        if (data.info != 'error') {
                            this.departments = data.info;
                        } else {
                            this.toast('获取部门数据失败！');
                        }
                    })
            }
        }
    }
</script>
